﻿using PdfiumViewer;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoWelding.Views.Documentation
{
    public partial class DocumentationForm : Form
    {
        public DocumentationForm()
        {
            InitializeComponent();
            pdfViewer1.Scroll += pdfViewer1_Scroll;
        }


        private void DocumentationForm_Load(object sender, EventArgs e)
        {
            ShowDocumentation();
            pdfViewer1.Scroll += pdfViewer1_Scroll;
        }

        /// <summary>
        /// 默认加载
        /// </summary>
        private void ShowDocumentation()
        {
            string filePath = "C:\\资料\\ER5000\\ER5000操作手册-中文.pdf";
            LoadPdfFile(filePath);

        }
        /// <summary>
        /// 加载PDF文件
        /// </summary>
        /// <param name="filePath"></param>
        private void LoadPdfFile(string filePath)
        {
            try
            {
                // 3、加载PDF文件并显示在 PdfViewer 控件中
                PdfDocument document = PdfDocument.Load(filePath);
                pdfViewer1.Document = document;

                //默认将文件放大1.5倍数
                pdfViewer1.Renderer.Zoom = 1.35;

                //获取文件总页数
                int pageCount = document.PageCount;
                label_totalPage.Text = pageCount.ToString();
                //当前页数
                label_nowPage.Text = "1";
            }
            catch (Exception ex)
            {
                // 4、错误处理
                MessageBox.Show($"无法打开PDF文件。\n\n错误信息: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 打开文件按钮的点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_open_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            // 设置对话框的标题
            openFileDialog.Title = "请选择PDF文件";
            // 设置文件过滤器，这样用户只能看到和选择.pdf文件
            openFileDialog.Filter = "PDF 文件 (*.pdf)|*.pdf|所有文件 (*.*)|*.*";
            // 设置默认显示的过滤器索引（这里默认选中第一个，即 "*.pdf"）
            openFileDialog.FilterIndex = 1;

            // 2、显示对话框，并检查用户是否点击了“打开”按钮
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                // 获取用户选择的文件的完整路径
                string filePath = openFileDialog.FileName;
                
                LoadPdfFile(filePath);
            }
        }

        /// <summary>
        /// 跳转页面按钮的点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_skipPage_Click(object sender, EventArgs e)
        {
            if (pdfViewer1.Document != null)
            {
                if (int.TryParse(txt_page.Text, out int pageNumber))
                {
                    // 检查输入是否在有效范围内
                    if (pageNumber >= 1 && pageNumber <= pdfViewer1.Document.PageCount)
                    {
                        // PdfiumViewer 的页码是从 0 开始的，所以要减 1
                        pdfViewer1.Renderer.Page = pageNumber - 1;
                        label_nowPage.Text = pageNumber.ToString();
                    }
                    else
                    {
                        MessageBox.Show($"请输入 1 到 {pdfViewer1.Document.PageCount} 之间的页码。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
                else
                {
                    MessageBox.Show("请输入有效的数字。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else
            {
                MessageBox.Show("请先打开一个PDF文件。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }


        private void pdfViewer1_Scroll(object sender, ScrollEventArgs e)
        {
            //if (pdfViewer1.Document == null)
            //    return;

            //double zoom = pdfViewer1.Renderer.Zoom;
            //var currentPage = pdfViewer1.Renderer.Page;
            //var pageSize = pdfViewer1.Document.PageSizes[currentPage];
            //double pageHeight = pageSize.Height * zoom;

            //int calculatedPage = (int)(pdfViewer1.VerticalScroll.Value / pageHeight);

            //if (calculatedPage >= 0 && calculatedPage < pdfViewer1.Document.PageCount)
            //{
            //    label_nowPage.Text = (calculatedPage + 1).ToString();
            //}

            MessageBox.Show("滚动事件");
        }

    }
}
